//
//  ServiceVC.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/7.
//
//

#import "ServiceVC.h"
#import "ServiceItemView.h"

@interface ServiceVC ()

@property (nonatomic, strong) WXPNavView * navView;
@property (nonatomic, strong) UIView * headerView;
@property (nonatomic, strong) ServiceItemView * phoneItemView;
@property (nonatomic, strong) ServiceItemView * emailItemView;
@property (nonatomic, strong) ServiceItemView * whatsItemView;
@property (nonatomic, strong) UIView * viewTips;
@property (nonatomic, strong) WXPCommonAgreementView * agreementView;

@property (nonatomic, strong) ModelService * modelDetail;

@end

@implementation ServiceVC

#pragma mark - lazy
- (WXPNavView *)navView
{
    if (_navView == nil) {
        _navView = [WXPNavView  new];
        _navView.backgroundColor = [UIColor clearColor];
        [_navView resetViewWithTitle:@"Servicio en línea"];
        _navView.btnService.hidden = self.isHideRightNavBtn;
        _navView.btnService.hidden = true;
        _navView.leftTop = XY(0, 0);
    }
    return  _navView;
}
- (UIView *)headerView
{
    if (_headerView == nil) {
        _headerView = [UIView  new];
        _headerView.userInteractionEnabled = true;
        _headerView.width = SCREEN_WIDTH-2*LEFT_MARDIN;
    }
    return  _headerView;
}
- (ServiceItemView *)phoneItemView
{
    if (_phoneItemView == nil) {
        _phoneItemView = [ServiceItemView  new];
        WEAKSELF
        _phoneItemView.blockItemClick = ^{
            [GlobalMethod hitPointWithPageName:KEY_CustomerService action:KEY_Click extInfo:nil];
            NSString *phoneNumber = weakSelf.modelDetail.phone;
            NSCharacterSet *allowedCharacters = [NSCharacterSet URLQueryAllowedCharacterSet];
            phoneNumber = [phoneNumber stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
            NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]];
            if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
                [[UIApplication sharedApplication] openURL:phoneURL options:@{} completionHandler:nil];
            } else {
                NSLog(@"设备不支持拨号功能");
            }
        };
    }
    return  _phoneItemView;
}
- (ServiceItemView *)emailItemView
{
    if (_emailItemView == nil) {
        _emailItemView = [ServiceItemView  new];
        WEAKSELF
        _emailItemView.blockItemClick = ^{
            [GlobalMethod hitPointWithPageName:KEY_CustomerService action:KEY_Click extInfo:nil];
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = weakSelf.modelDetail.email;
            [GlobalMethod showAlert:@"Copia éxito"];
        };
    }
    return  _emailItemView;
}
- (ServiceItemView *)whatsItemView
{
    if (_whatsItemView == nil) {
        _whatsItemView = [ServiceItemView  new];
        WEAKSELF
        _whatsItemView.blockItemClick = ^{
            [GlobalMethod hitPointWithPageName:KEY_CustomerService action:KEY_Click extInfo:nil];
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = weakSelf.modelDetail.whatsApp;
            [GlobalMethod showAlert:@"Copia éxito"];
        };
    }
    return  _whatsItemView;
}
- (UIView *)viewTips
{
    if (_viewTips == nil) {
        _viewTips = [UIView  new];
        _viewTips.backgroundColor = [UIColor clearColor];
        UILabel * label = [UILabel new];
        label.fontNum = 12;
        label.textColor = COLOR_LIGHT_GRAY;
        label.numberOfLines = 0;
        label.lineSpace = 3;
        [label fitTitle:@"Por favor, envíenos su nombre y apellido, número de teléfono y el nombre de la aplicación cuando nos proporcione cualquier retroalimentación para que podamos verificar los casos. ¡Gracias!" variable:self.headerView.width-2*20];
        [_viewTips addSubview:label];
        _viewTips.leftTop = XY(20, 30);
        _viewTips.height = label.bottom+20;
    }
    return  _viewTips;
}
- (WXPCommonAgreementView *)agreementView{
    if (!_agreementView) {
        _agreementView = [WXPCommonAgreementView new];
        _agreementView.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-LEFT_MARDIN);
    }
    return _agreementView;
}

#pragma mark - viewDidLoad
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    // 打点
    [GlobalMethod hitPointWithPageName:KEY_CustomerService action:KEY_Enter extInfo:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self requestList];
}

- (void)configUI{
    [self.view addSubview:self.agreementView];
    self.viewBG.backgroundColor = COLOR_BACKGROUND;
    self.tableView.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:self.navView];
    self.tableView.width = SCREEN_WIDTH-2*LEFT_MARDIN;
    self.tableView.centerX = SCREEN_WIDTH/2;
    self.tableView.height = self.agreementView.top-10-self.navView.bottom-10;
    self.tableView.top = self.navView.bottom+10;
}

#pragma mark - resetHeaderView
- (void)resetHeaderView{
    self.headerView = [UIView initWithViews:@[
        self.phoneItemView,
        self.emailItemView,
        self.whatsItemView,
        self.viewTips,
    ]];
    self.headerView.backgroundColor = COLOR_WHITE;
    [GlobalMethod setRoundView:self.headerView color:[UIColor clearColor] numRound:16 width:0];
    self.tableView.tableHeaderView = self.headerView;
}

#pragma mark - request
- (void)requestList{
    [RequestApi requestCustomerServiceInfoWithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        self.modelDetail = [GlobalMethod exchangeDicToModel:response modelName:@"ModelService"];
        // 电话
        ModelBtn * phoneModel = [ModelBtn new];
        phoneModel.content = readStr(self.modelDetail.phone);
        phoneModel.title = @"Llame a la línea directa de servicio al cliente";
        phoneModel.imageName = @"icon-service-phone";
        phoneModel.highImageName = @"icon-service-phone-tail";
        [self.phoneItemView resetViewWithModel:phoneModel];
        
        // 邮箱
        NSString * maskedEmail = self.modelDetail.email;
//        NSArray *components = [maskedEmail componentsSeparatedByString:@"@"];
//        if (components.count == 2) {
//            NSString *prefix = components[0];
//            NSString *maskedString = [@"" stringByPaddingToLength:prefix.length withString:@"*" startingAtIndex:0];
//            maskedEmail = [maskedString stringByAppendingString:[NSString stringWithFormat:@"@%@", components[1]]];
//        }
        ModelBtn * mailModel = [ModelBtn new];
        mailModel.content = readStr(maskedEmail);
        mailModel.title = @"Correo electrónico de servicio al cliente";
        mailModel.imageName = @"icon-service-email";
        mailModel.highImageName = @"icon-service-email-tail";
        [self.emailItemView resetViewWithModel:mailModel];
        
        // whatsApp
        NSString * whatsAppStr = self.modelDetail.whatsApp;
        ModelBtn * whatsAppModel = [ModelBtn new];
        whatsAppModel.content = readStr(whatsAppStr);
        whatsAppModel.title = @"Whats App";
        whatsAppModel.imageName = @"icon-service-whats";
        whatsAppModel.highImageName = @"icon-service-email-tail";
        [self.whatsItemView resetViewWithModel:whatsAppModel];
        
        [self resetHeaderView];
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

@end
